<?php

main();


function main(){
    $key = "my32digitkey12345678901234567890";
    $plainText = "hellow word!";

    $privateKey = '-----BEGIN RSA PRIVATE KEY-----
MIICXwIBAAKBgQC3oSDe9Gu6AcoNU0NYQRBi3Pidwqlet/PpMddqlqnUO4sP4R0/
ABOHbf/1byVbfKsbpEQqan2+v8x7MvrjZtzl6cAqrGkp3zmfvMHSkYBaQFZym0Hc
49sMCbygCy77Hw9PnXsFIFayTsT97Whd6U8HzKg51wHoSW+eq2QmjZUCsQIDAQAB
AoGBAIHnyFRR/5/gQit5GuxlYw09m9gnbSBn7HKtYuKx4UUWNoOuUr1N9YLai7f6
wCffo0DzzZFgMsLt9t+1Kg4Tp/L/Z9n1zOIDTViETZrLChzpbaIf413d0kd7uwJd
R4L5+adKWI7KNfHiuiJOycz7njhqQGVr30U2GNRRsL0YBhhRAkEA5BIbi10bpRnC
ZCCM+sABXJPnoGwpJHZ5Q+Ntqsq5NbgBmQPZYisK5jgGUuDR1vuMkFnwve7IqEc1
So9yiYQLnQJBAM4dz0SGRzhu0GkzIvbwXRxakTjQAVH2z/mF1JnRQkMelymkwb7A
c1/N4/gWbphAoVwjnbc19YqhG2HUePPBGSUCQQDeL53R6UUTVMMCFIwDhKZO8HBI
4tY6BYkh0CB4sMI6SSaVUSCn+FLH8XCHsSn8jFdmEZjtEAE/nw+VsaXdvlwpAkEA
wWd5UExLUemxR7VDDsFWLT/SWqPbSUS1u+ZXKooPihmPL/U4EzxURkZUrjqmRdkH
UATffcV1BELOBcswP1EmvQJBALpQG6QqeFzeCjFer1LyNDyhENeceyDsJ32T9X6y
ZsfY0Y4XYNzLWsLGNsG5DT8p958wBytqZ/cnk2Kzes8RREQ=
-----END RSA PRIVATE KEY-----
'; 
$publicKey = '-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC3oSDe9Gu6AcoNU0NYQRBi3Pid
wqlet/PpMddqlqnUO4sP4R0/ABOHbf/1byVbfKsbpEQqan2+v8x7MvrjZtzl6cAq
rGkp3zmfvMHSkYBaQFZym0Hc49sMCbygCy77Hw9PnXsFIFayTsT97Whd6U8HzKg5
1wHoSW+eq2QmjZUCsQIDAQAB
-----END PUBLIC KEY-----';

    if ('rsa'!='') {
        // $pu_key = openssl_pkey_get_public($publicKey);
        // var_dump($pu_key);exit;
        print("-----------------rsaEncrypt------start---") ;

        $encode=rsaEncrypt($plainText,$publicKey);
        $decode=rsaDecrypt($encode,$privateKey);

        printf("\nplainText:%s\nencode:%s\ndecode:%s\n",$plainText,$encode,$decode);

        print("-----------------rsaEncrypt------end---\n\n");
    }


    if ('aes'!='') {
        print("-----------------aesEncrypt------start---");

        $encrypted= aesEncrypt($plainText, $key);
        $decrypted= aesDecrypt($encrypted, $key);
        printf("\nplainText:%s\nencrypted:%s\ndecrypted:%s", $plainText, $encrypted, $decrypted);
        
        print("\n-----------------aesEncrypt------end---\n");
    }

    printf(
        "\nbase64_encode:%s\nplainText:%s\nmd5:%s", 
        base64_encode($plainText),
        base64_decode(base64_encode($plainText)),
        md5($plainText)
    );
}

function fixIv($iv='')  {
	$iv = $iv . '1234567890123456';
    $iv = substr($iv,0,16);

	return $iv;
}

function aesDecrypt($encrypted,$key,$iv=''){
    // https://news.sangniao.com/p/3723057230
    
    $iv = fixIv($iv);
    $decrypted = openssl_decrypt(
        $encrypted,
        'AES-256-CBC',
        $key,
        0,
        $iv
    );

    return $decrypted;
}

function aesEncrypt($plainText,$key,$iv=''){
    // https://news.sangniao.com/p/3723057230

    $iv = fixIv($iv);

    $encrypted = openssl_encrypt(
        $plainText,
        'AES-256-CBC',
        $key,
        0,
        $iv
    );

    return $encrypted;
}


function rsaEncrypt($data, $publicKey)
{
    // https://www.yii666.com/blog/129530.html

    openssl_public_encrypt($data, $crypted, $publicKey);
    $bs64=base64_encode($crypted);

    return $bs64;
}

function rsaDecrypt($data, $privateKey)
{
    // https://www.yii666.com/blog/129530.html

    $bs64=base64_decode($data);
    openssl_private_decrypt($bs64, $decrypted, $privateKey);

    return $decrypted;
}
